import './lib/FlowLine.js';

export default class Flypath {
  constructor(basicCesium) {
    this.basicCesium = basicCesium
    this.viewer = basicCesium.viewer;
    this.entities = this.viewer.entities;

    this._init();
  }

  //抛物飞线效果初始化
  _init() {
    //每条线上的飞线数量
    let num = 3;
    let center = [118.804722, 31.976727];
    let positions = [
      [115.724183, 39.567735],
      [111.430149, 35.025655],
      [110.986794, 29.295230],
      [118.9236839, 32.628061],
      [118.8236839, 32.428061],
      [119.0236839, 32.628061],
      [118.8236839, 32.628061],
      [119.0236839, 32.428061]
    ];
    positions.forEach(item => {
      let fly = this.parabola(center, item, 10000);
      this.getFlashPoint(fly[fly.length - 1]);
      // 创建飞线
      for (let i = 0; i < num; i++) {
        this.viewer.entities.add({
          polyline: {
            positions: fly,
            material: new Cesium.FlowLineMaterialProperty({
              color: Cesium.Color.fromCssColorString('#ff0'),
              speed: 15 * Math.random(),
              percent: 0.1,
              gradient: 0.01
            })
          },
        });
      }
      // 创建轨迹线
      this.viewer.entities.add({
        polyline: {
          positions: fly,
          material: new Cesium.Color(1.0, 1.0, 0.0, 0.3),
          width: 3.0
        }
      })
    });
    this.viewer.zoomTo(this.entities.values)

  }

  //绘制闪烁点
  getFlashPoint(position, color = Cesium.Color.fromCssColorString('#00eaff'), outlineColor = Cesium.Color.fromCssColorString('#A64968')) {
    let markerOpacity = 1.0;
    let outLineOpacity = 1.0;
    let outlineWidth = 1;
    let outlineWidthMax = 4;
    let a = true,
      n = true,
      o = true;
    let option = {
      color: new Cesium.CallbackProperty(() => {
        return a ? (markerOpacity -= .005,
            markerOpacity <= 0.5 && (a = false)) : (markerOpacity = 1,
            a = true),
          color.withAlpha(markerOpacity)
      }, false),
      pixelSize: 8,
      outlineColor: new Cesium.CallbackProperty(() => {
        return o ? (outLineOpacity -= .005,
            outLineOpacity <= 0.1 && (o = false)) : (outLineOpacity = 0.8,
            o = true),
          outlineColor.withAlpha(outLineOpacity)
      }, false),
      outlineWidth: new Cesium.CallbackProperty((time, result) => {
        return n ? (outlineWidth += 0.05,
            outlineWidth >= outlineWidthMax && (n = false)) : (outlineWidth = 8,
            n = true),
          outlineWidth
      }, false)
    }
    this.viewer.entities.add({
      position: position,
      point: option
    })
  }

  //抛物线构造函数（参考开源代码）
  parabola(start, end, height = 0, count = 50) {
    //方程 y=-(4h/L^2)*x^2+h h:顶点高度 L：横纵间距较大者
    let result = []
    height = Math.max(+height, 0)
    count = Math.max(+count, 50)
    let diffLon = Math.abs(start[0] - end[0])
    let diffLat = Math.abs(start[1] - end[1])
    let L = Math.max(diffLon, diffLat)
    let dlt = L / count
    if (diffLon > diffLat) {
      //base on lon
      let delLat = (end[1] - start[1]) / count
      if (start[0] - end[0] > 0) {
        dlt = -dlt
      }
      for (let i = 0; i < count; i++) {
        let h =
          height -
          (Math.pow(-0.5 * L + Math.abs(dlt) * i, 2) * 4 * height) /
          Math.pow(L, 2)
        let lon = start[0] + dlt * i
        let lat = start[1] + delLat * i
        let point = new Cesium.Cartesian3.fromDegrees(lon, lat, h);
        result.push(point);
      }
    } else {
      //base on lat
      let delLon = (end[0] - start[0]) / count
      if (start[1] - end[1] > 0) {
        dlt = -dlt
      }
      for (let i = 0; i < count; i++) {
        let h =
          height -
          (Math.pow(-0.5 * L + Math.abs(dlt) * i, 2) * 4 * height) /
          Math.pow(L, 2)
        let lon = start[0] + delLon * i
        let lat = start[1] + dlt * i
        let point = new Cesium.Cartesian3.fromDegrees(lon, lat, h);
        result.push(point);
      }
    }
    return result
  }
}